#ifndef __GLES2HardwareBufferManager_H__
#define __GLES2HardwareBufferManager_H__

#include "HardwareBufferManager.h"

namespace PVM
{
	#define GL_DEFAULT_MAP_BUFFER_THRESHOLD (1024 * 32)
	
	class GLES2HardwareBufferManagerBase : public HardwareBufferManagerBase
	{
	protected:
		char* mScratchBufferPool;
		PVM_MUTEX(mScratchMutex);
		size_t mMapBufferThreshold;
		
	public:
		GLES2HardwareBufferManagerBase();
		virtual ~GLES2HardwareBufferManagerBase();
		
		HardwareVertexBufferSharedPtr createVertexBuffer(size_t vertexSize,
				size_t numVerts, HardwareBuffer::Usage usage, bool useShadowBuffer = false);
		
		HardwareVertexBufferSharedPtr createIndexBuffer(
			HardwareIndexBuffer::IndexType itype, size_t numIndexes,
			HardwareBuffer::Usage usage, bool useShadowBuffer = false);
		RenderToVertexBufferSharedPtr createRenderToVertexBuffer();
		static GLenum getGLUsage(unsigned int usage);
		static GLenum getGLType(unsigned int type);
		void* allocateScratch(uint32 size);
		void deallocateScratch(void* ptr);
		const size_t getGLMapBufferThreshold() const;
		void setGLMapBufferThreshold(const size_t value);
	};
	
	class GLES2HardwareBufferManager : public HardwareBufferManager
	{
	public:
		GLES2HardwareBufferManager()
		: HardwareBufferManager(new GLES2HardwareBufferManagerBase())
		{
		}
		
		~GLES2HardwareBufferManager()
		{
			delete mImpl;
		}
		
		static GLenum getGLUsage(unsigned int usage)
		{
			return GLES2HardwareBufferManagerBase::getGLUsage(usage);
		}
		
		static GLenum getGLType(unsigned int type)
		{
			return GLES2HardwareBufferManagerBase::getGLType(type);
		}
		
		void* allocateScratch(uint32 size)
		{
			return static_cast<GLES2HardwareBufferManagerBase*>(mImpl)->allocateScratch(size);
		}
		
		void deallocateScratch(void* ptr)
		{
			static_cast<GLES2HardwareBufferManagerBase*>(mImpl)->deallocateScratch(ptr);
		}
		
		const size_t getGLMapBufferThreshold() const
		{
			return static_cast<GLES2HardwareBufferManagerBase*>(mImpl)->getGLMapBufferThreshold();
		}
		
		void setGLMapBufferThreshold(const size_t value)
		{
			static_cast<GLES2HardwareBufferManagerBase*>(mImpl)->setGLMapBufferThreshold(value);
		}
	};
}

#endif
